const {Controller} = require('uni-cloud-router')
const uniID = require('uni-id')
module.exports = class UserController extends Controller {
    async loginOrRegister() {
        const {username, mobile, yzmCode} = this.ctx.data
        return this.service.user.loginOrRegister({username, mobile, yzmCode})
    }

    async sendMobileCode() {
        const {mobile} = this.ctx.data
        return this.service.user.sendMobileCode(mobile)
    }

    async login() {
        const {username, password} = this.ctx.data
        return this.service.user.login({username, password})
    }

    async userLogin() {
        const {username, password} = this.ctx.data
        return this.service.user.userLogin({username, password})
    }

    async register() {
        const {username, password} = this.ctx.data
        const admin = await this.service.user.hasAdmin()
        if (admin) {
            return {code: 10001, message: '超级管理员已存在，请登录...'}
        }
        return uniID.register({username, password, role: ["admin"]})
    }

    async logout() {
        return this.service.user.logout(this.ctx.event.uniIdToken)
    }

    async getUserList() {
        return this.service.user.getUserList()
    }
}
